package com.snow.useviewdemo.function.suspension;

import android.graphics.Color;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import com.flyco.tablayout.listener.OnTabSelectListener;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.snow.common.base.CustomViewPagerAdapter;
import com.snow.common.tool.utils.MeasureUtils;
import com.snow.useviewdemo.R;
import com.snow.useviewdemo.base.BaseActivity;
import com.snow.useviewdemo.databinding.ActivitySuspensionBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * author : snowzhao
 * e-mail : zyt@ttsing.com
 * date   : 2020/4/23
 * desc   :滑动悬浮tab功能实现
 */
public class SuspensionActivity extends BaseActivity<ActivitySuspensionBinding> {
    private CustomViewPagerAdapter mAdapter;
    private List<Fragment> mFragmentList = new ArrayList<>();
    private List<String> mTitleList = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_suspension;
    }

    @Override
    protected void setViewData(Bundle savedInstanceState) {
        initData();
        setSort();
        initListener();
    }

    private void initListener() {
        mBinding.toolbar.setBackgroundColor(getResources().getColor(R.color.transparent));
        mBinding.appbarLayout.addOnOffsetChangedListener((appBarLayout, verticalOffset) -> {
            if (verticalOffset == 0) {
                mBinding.ivBack.setImageResource(R.mipmap.ic_back);
                mBinding.tvTitle.setText("");
            } else if (Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange()) {
                mBinding.ivBack.setImageResource(R.mipmap.ic_back);
                mBinding.tvTitle.setText("免费专区");
            } else {

            }
            mBinding.toolbar.setBackgroundColor(
                    changeAlpha(getResources().getColor(R.color.white), Math.abs(verticalOffset * 1.0f) / appBarLayout.getTotalScrollRange()));
        });

        mBinding.slideBar.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                if (mTitleList != null && position < mTitleList.size()) {
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }

    private void initData() {


    }

    private void setSort() {
        String[] titleStr = new String[]{"热门诗人", "主题", "风格", "时令", "年纪"};
        for (String name : titleStr) {
            mTitleList.add(name);
            mFragmentList.add(new FreeListFragment());
        }
        mAdapter = new CustomViewPagerAdapter(getSupportFragmentManager(), mFragmentList, mTitleList);
        mBinding.vpContent.setAdapter(mAdapter);
        mBinding.slideBar.setViewPager(mBinding.vpContent);
        mBinding.vpContent.setOffscreenPageLimit(mAdapter.getCount());
        mBinding.vpContent.setCurrentItem(0);
        mBinding.slideBar.setCurrentTab(0);
//        mBinding.slideBar.updateTabSelection(0);
    }

    /**
     * 根据百分比改变颜色透明度
     */
    public int changeAlpha(int color, float fraction) {
        int red = Color.red(color);
        int green = Color.green(color);
        int blue = Color.blue(color);
        int alpha = (int) (Color.alpha(color) * fraction);
        return Color.argb(alpha, red, green, blue);
    }
}
